import argparse
import numpy as np

from collections import defaultdict

def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--m', type=str, default="/home/xiaoyifu/data/HG002/R9.4/deepsignal2/part1.CG.deepsignal2_chm13_retrain.call_mods.methylated.tsv", required=False,
                        help="input data")#/home/xiaoyifu/data/HG002/R10.4/20221109_1654_5A_PAG65784_f306681d/test1/fast5s.CG.call_mods.21mer.tsv
    parser.add_argument('--um', type=str, default="/home/xiaoyifu/data/HG002/R9.4/deepsignal2/part1.CG.deepsignal2_chm13_retrain.call_mods.unmethylated.tsv", required=False,
                        help="input data")#/home/xiaoyifu/data/HG002/R10.4/20221109_1654_5A_PAG65784_f306681d/test1/fast5s.CG.call_mods.21mer.tsv
    parser.add_argument('--bed', type=str, default="/home/xiaoyifu/data/HG002/Bisulfite/non_singleton.bisulfite.kmer.13.bed", required=False,
                        help="bed")
    return parser.parse_args()

def read_file(m_file,um_file,input_bed,prob_cf=0):
    bedsites=defaultdict(set)
    #tsvsites=defaultdict(set)
    msites=set()
    pred_true=0
    pred_false=0
    with open(input_bed, 'r') as rf:
        for line in rf:
            words = line.strip().split("\t")
            #if words[0] not in sites.keys():
            #    sites[words[0]]=set()
            bedsites[(words[0],words[5])].add(int(words[1]))
            key=(words[0],words[5],int(words[1]))
            msites.add(key)
    with open(m_file, 'r') as rf:
        for line in rf:
            words = line.strip().split("\t")
            key=(words[0],words[2],int(words[1]))
            if key not in msites:
                continue
            test_value=1 if (float(words[7])-float(words[6]))>prob_cf else 0
            if test_value==1:
                pred_true+=1
            else:
                pred_false+=1
    with open(um_file, 'r') as rf:
        for line in rf:
            words = line.strip().split("\t")
            key=(words[0],words[2],int(words[1]))
            if key not in msites:
                continue
            test_value=1 if (float(words[7])-float(words[6]))>prob_cf else 0
            if test_value==0:
                pred_true+=1
            else:
                pred_false+=1
    print('pred true: {}, pred false: {}'.format(pred_true,pred_false))

if __name__ == '__main__':
    args=parse_args()
    #read_file(args.bed,args.output+'consensus_non_singleton_bed.tsv')
    read_file(args.m,args.um,args.bed)